package com.horoscope.astrology.zodiac.signs.free.service

import android.content.Context
import android.util.Log
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.utils.NotificationUtil
import java.util.*

/**
 *  作者    1024
 *  时间    2020/5/28 下午1:36
 *  文件    项目备份
 *  描述    name  relation  birthChat 本地闹钟，每48小时执行一次，判断是否在下午2点到5点间，展示最新数据到通知
 */

class NameWork(private val mContext: Context, private val mParams: WorkerParameters) :
    Worker(mContext, mParams) {

    // 具体执行逻辑
    override fun doWork(): Result {
        Log.e("wangshuo", "进入了dowork")
        if (compareCurrentHour(mParams.inputData.getIntArray("num"))) {
            Log.e("wangshuo", "时间准确")
            if (AppSettingManager.readInt("isFirstOpen", 0) != 0) {
                // 第一次打开应用不显示闹钟
                when (mParams.inputData.getString("workName")) {
                    "NameAlarm" -> getWorkName(1011)
                    "RelationAlarm" -> getWorkName(1012)
                    "BirthChatAlarm" -> getWorkName(1013)
                }
                Log.e("wangshuo", "显示通知")
            } else {
                Log.e("wangshuo", "第一次不显示通知")
            }
        } else {
            Log.e("wangshuo", "时间不准确")
            return Result.retry()
        }
        return Result.success()
    }

    // 判断时间段
    private fun compareCurrentHour(num: IntArray?): Boolean {
        if (num != null) {
            val current = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
            return current == num[0] || current == num[1] || current == num[2]
        }
        return true
    }

    // 根据传递过来的参数，创建不同的闹钟
    private fun getWorkName(alarmType: Int) {
        NotificationUtil.createThree(
            mContext,
            alarmType,
            mParams.inputData.getString("workName").toString()
        )
    }

}